﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using brjostagjof.Models;
using System.Diagnostics;

namespace brjostagjof.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        IDataRepository rep = null;

        #region Articles

        public ActionResult Greinar()
        {
            IEnumerable<Article> result = rep.GetAllArticles();
            return View(result);
        }

        [HttpGet]
        [Authorize(Roles="Administrators, Specialists")]
        public ActionResult AddArticle()
        {
            return View(new Article());
        }

        [HttpPost]
        [Authorize(Roles="Administrators, Specialists")]
        [ValidateInput(false)]
        public ActionResult AddArticle(Article unused)
        {
            FormCollection formData = new FormCollection(Request.Form);

            if (!string.IsNullOrEmpty(formData["headline"]) && !string.IsNullOrEmpty(formData["context"]))
            {
                InformationBase infoBase = new InformationBase();
                infoBase.headline = formData["headline"];
                infoBase.context = formData["context"];
                infoBase.date = DateTime.Now;

                ArticleBase artBase = new ArticleBase();
                artBase.articleCategoryID = int.Parse(formData["category"]);
                artBase.link = "/Home/ReadArticle/";
                artBase.userID = (rep.GetUserByName(User.Identity.Name)).ID;
                if (formData["displayed"] == "true")
                {
                    artBase.displayed = true;
                }
                else
                {
                    artBase.displayed = false;
                }

                Article article = new Article();
                ArticleContainer result = new ArticleContainer { infoBase = infoBase, artBase = artBase, article = article };
                rep.AddArticle(result);
                return RedirectToAction("Greinar");
            }
            else
            {
                return View(unused);
            }
        }

        [HttpGet]
        [Authorize(Roles="Administrators")]
        public ActionResult AddArticleCategory()
        {
            return View(new ArticleCategory());
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddArticleCategory(FormCollection formData)
        {
            ArticleCategory artcat = new ArticleCategory();
            artcat.name = formData["category"];
            artcat.type = "Articles";
            rep.AddArticleCategory(artcat);
            return View("Greinar");
        }

        [HttpGet]
        public static IEnumerable<Article> Get5Articles()
        {
            DataRepository rep = new DataRepository();
            IEnumerable<Article> result = rep.ArticlesLatestFive();
            return result;
        }

        [HttpGet]
        public static IEnumerable<Article> Get10Articles()
        {
            DataRepository rep = new DataRepository();
            IEnumerable<Article> result = rep.ArticlesLatestTen();
            return result;
        }

        [HttpGet]
        public ActionResult ReadArticle(int? ID)
        {
            if(ID.HasValue)
            {
                Article result = rep.GetArticleByID(ID.Value);
                if(result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        [Authorize(Roles="Administrators, Specialists")]
        public ActionResult EditArticle(int? ID)
        {
            if(ID != null)
            {
                var model = rep.GetArticleByID(ID.Value);

                if(model == null)
                {
                    return View("Not Found");
                }
                else
                {
                    ViewData["Listitems"] = rep.GetArticleList();
                    return View(model);
                }
            }
            else
            {
                return View("Not Found");
            }
        }

        [HttpPost]
        [Authorize(Roles="Administrators, Specialists")]
        [ValidateInput(false)]
        public ActionResult EditArticle(Article formdata)
        {
            FormCollection check = new FormCollection(Request.Form);
            if (!string.IsNullOrEmpty(check["ArticleBase.InformationBase.headline"]) && !string.IsNullOrEmpty(check["ArticleBase.InformationBase.context"]))
            {
                rep.UpdateArticle(formdata);

                return RedirectToAction("Greinar");
            }
            ViewData["Listitems"] = rep.GetArticleList();
            return View(formdata);
        }

        [HttpGet]
        public ActionResult GetArticleByCategoryID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetArticleByCategoryID(ID.Value);
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public ActionResult Get10ArticleByPageID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.Get10ArticleByPageID(ID.Value);
                ViewData["pageID"] = ID;
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public static int GetAllArticleCount()
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetAllArticles();
            int count = result.Count();
            return count;
        }

        [HttpGet]
        public ActionResult GetArticlesByCategoryIDHeadlineAlphabetical(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetArticlesByCategoryIDHeadlineAlphabetical(ID.Value);
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public static IEnumerable<ArticleCategory> GetArticleCategories()
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetArticleCategories();
            return result;
        }

        [HttpPost]
        [Authorize(Roles="Administrators")]
        public ActionResult DeleteArticle(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetArticleByID(ID.Value);
                rep.DeleteArticle(result);
                if (result == null)
                    return View("Not Found");
                return View("Greinar");
            }
            return View("Not Found");
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult HiddenArticles()
        {
            IEnumerable<Article> result = rep.GetHiddenArticles();
            return View(result);
        }

        #endregion

        #region News

        public ActionResult Frettir()
        {
            IEnumerable<New> result = rep.GetAllNews();
            return View(result);
        }
            

        [HttpGet]
        [Authorize(Roles="Administrators, Specialists")]
        public ActionResult AddNews()
        {
            return View(new New());
        }

        [HttpPost]
        [Authorize(Roles="Administrators, Specialists")]
        [ValidateInput(false)]
        public ActionResult AddNews(New unused)
        {
            FormCollection formData = new FormCollection(Request.Form);

            if (!string.IsNullOrEmpty(formData["headline"]) && !string.IsNullOrEmpty(formData["context"]))
            {
                InformationBase infoBase = new InformationBase();
                infoBase.headline = formData["headline"];
                infoBase.context = formData["context"];
                infoBase.date = DateTime.Now;

                ArticleBase artBase = new ArticleBase();
                artBase.articleCategoryID = int.Parse(formData["category"]);
                artBase.link = "/Home/ReadNews/";
                artBase.userID = (rep.GetUserByName(User.Identity.Name)).ID;
                if (formData["displayed"] == "true")
                {
                    artBase.displayed = true;
                }
                else
                {
                    artBase.displayed = false;
                }

                New news = new New();
                NewsContainer result = new NewsContainer { infoBase = infoBase, artBase = artBase, news = news };
                rep.AddNews(result);
                return RedirectToAction("Frettir");
            }
            else
            {
                return View(unused);
            }
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddNewsCategory()
        {
            return View(new ArticleCategory());
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddNewsCategory(FormCollection formData)
        {
            ArticleCategory artcat = new ArticleCategory();
            artcat.name = formData["category"];
            artcat.type = "News";
            rep.AddArticleCategory(artcat);
            return View("Frettir");
        }

        [HttpGet]
        [Authorize(Roles="Administrators")]
        public ActionResult EditNews(int? id)
        {
            if(id != null)
            {
                var model = rep.GetNewsByID(id.Value);

                if(model == null)
                {
                    return View("Not Found");
                }
                else
                {
                    ViewData["Listitems"] = rep.GetNewsList();
                    return View(model);
                }
            }
            else
            {
                return View("Not Found");
            }
        }

        [HttpPost]
        [Authorize(Roles="Administrators")]
        [ValidateInput(false)]
        public ActionResult EditNews(New formData)
        {
            FormCollection check = new FormCollection(Request.Form);
            if(!string.IsNullOrEmpty(check["ArticleBase.InformationBase.headline"]) && !string.IsNullOrEmpty(check["ArticleBase.InformationBase.context"]))
            {
                rep.UpdateNews(formData);

                return RedirectToAction("Frettir");
            }
            ViewData["Listitems"] = rep.GetNewsList();
            return View(formData);

        }

        [HttpGet]
        public ActionResult GetNewsByCatagoryID(int? ID)
        {
            if(ID.HasValue)
            {
                var category=rep.GetArticleCategoryByID(ID.Value);
                var result=rep.GetArticleByCategory(category);
                return View(result);
            }
            return View("Not found");
        }

        [HttpGet]
        public ActionResult GetNewsByCategoryID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetNewsByCategoryID(ID.Value);
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public ActionResult GetNewsByCategoryIDHeadlineAlphabetical(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetNewsByCategoryIDHeadlineAlphabetical(ID.Value);
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public static IEnumerable<ArticleCategory> GetNewsCategories()
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetNewsCategories();
            return result;
        }

        [HttpGet]
        public static int GetAllNewsCount()
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetAllNews();
            int count = result.Count();
            return count;
        }

        [HttpGet]
        public ActionResult Get10NewsByPageID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.Get10NewsByPageID(ID.Value);
                ViewData["pageID"] = ID;
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpPost]
        [Authorize(Roles="Administrators")]
        public ActionResult DeleteNews(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetNewsByID(ID.Value);
                rep.DeleteNews(result);
                if (result == null)
                    return View("Not Found");
                return RedirectToAction("Frettir");
            }
            return View("Not Found");
        }

        [HttpGet]
        public static IEnumerable<New> Get5News()
        {
            DataRepository rep = new DataRepository();
            IEnumerable<New> result = rep.NewsLatestFive();
            return result;
        }

        [HttpGet]
        public static IEnumerable<New> Get10News()
        {
            DataRepository rep = new DataRepository();
            IEnumerable<New> result = rep.NewsLatestTen();
            return result;
        }

        [HttpGet]
        public ActionResult ReadNews(int? ID)
        {
            if(ID.HasValue)
            {
                New result = rep.GetNewsByID(ID.Value);
                if(result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public ActionResult HiddenNews()
        {
            IEnumerable<New> result = rep.GetHiddenNews();
            return View(result);
        }

        #endregion

        #region Frontpage

        public ActionResult Index()
        {
            IEnumerable<ArticleBase> top10ArticleBases = rep.FrontPageLatestTen();
            List<TopTenArticles> topTen = new List<TopTenArticles>();
            foreach(var model in top10ArticleBases)
            {
                string [] previewContext=model.InformationBase.context.Split(null);
                string preview = "";
                int i = 0;
                foreach(string item in previewContext)
                {
                    if(i>100)
                    {
                        preview+=" ...";
                        break;
                    }
                    preview+= item + " ";
                    i++;
                }
                topTen.Add(new TopTenArticles { ID = model.informationBaseID, date = model.InformationBase.date.ToString("dd. MMMM HH:mm"), context=preview, headline=model.InformationBase.headline, category=model.ArticleCategory.name, skrifadAf=model.User.name, catLink=model.link, auhtorID=model.InformationBase.ArticleBase.userID});
            }
            return View(topTen);
        }

        #endregion

        #region Specialists

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddSpecialistCategory()
        {
            return View(new SpecialistCategory());
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddSpecialistCategory(FormCollection formData)
        {
            SpecialistCategory specat = new SpecialistCategory();
            specat.name = formData["category"];
            rep.AddSpecialistCategory(specat);
            return RedirectToAction("FagAdilar");
        }

        public ActionResult FagAdilar()
        {
            IEnumerable<SpecialistUser> result = rep.GetSpecialists();
            return View(result);
        }

        [HttpGet]
        public static IEnumerable<SpecialistCategory> GetSpecialistCategories()
        {
            DataRepository rep = new DataRepository();
            return rep.GetSpecialistGategories();
        }

        [HttpGet]
        public ActionResult GetSpecialistByCategoryID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetSpecialistsByCategoryID(ID.Value);
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public ActionResult GetSpecialistByID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetSpecialistByID(ID.Value);
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public static IEnumerable<SpecialistUser> GetAllSpecialist()
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetSpecialists();
            return result;
        }

        #endregion

        #region Chat

        public ActionResult Spjall()
        {
            return View();
        }

        #endregion

        #region Q'n'A

        public ActionResult SogS()
        {
            IEnumerable<Question> result = rep.GetAllQuestions();
            return View(result);
        }

        [HttpGet]
        public static IEnumerable<Question> Get5Questions()
        {
            DataRepository rep = new DataRepository();
            IEnumerable<Question> result = rep.QuestionsLatestFive();
            return result;
        }

        [HttpGet]
        public static IEnumerable<Question> Get10Questions()
        {
            DataRepository rep = new DataRepository();
            IEnumerable<Question> result = rep.QuestionsLatestTen();
            return result;
        }

        [HttpGet]
        public ActionResult ReadQuestion(int? ID)
        {
            if(ID.HasValue)
            {
                Question result = rep.GetQuestionByID(ID.Value);
                if(result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("NotFound");
        }

        [HttpGet]
        public ActionResult AddQuestion()
        {
            return View(new Question());
        }

        [HttpPost]
        public ActionResult AddQuestion(Question unused)
        {
            FormCollection formData = new FormCollection(Request.Form);
            if (!string.IsNullOrEmpty(formData["headline"]))
            {
                string context = formData["context"];
                if (string.IsNullOrEmpty(context))
                {
                    context = "";
                }
                InformationBase infoBase = new InformationBase();
                infoBase.headline = formData["headline"];
                infoBase.context = formData["context"];
                infoBase.date = DateTime.Now;

                ArticleBase artBase = new ArticleBase();
                artBase.articleCategoryID = int.Parse(formData["category"]);
                artBase.link = "/Home/ReadQuestion/";
                artBase.userID = (rep.GetUserByName(User.Identity.Name)).ID;
                artBase.displayed = false;

                Question question = new Question();
                QuestionContainer result = new QuestionContainer { infoBase = infoBase, artBase = artBase, question = question };
                rep.AddQuestion(result);
                return RedirectToAction("SogS");
            }
            else
            {
                return View(unused);
            }
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddQuestionsCategory()
        {
            return View(new ArticleCategory());
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddQuestionsCategory(FormCollection formData)
        {
            ArticleCategory artcat = new ArticleCategory();
            artcat.name = formData["category"];
            artcat.type = "Questions";
            rep.AddArticleCategory(artcat);
            return View("SogS");
        }

        [HttpGet]
        public ActionResult EditQuestion(int? ID)
        {
            if(ID != null)
            {
                var model = rep.GetQuestionByID(ID.Value);

                if(model == null)
                {
                    return View("Not Found");
                }
                else
                {
                    ViewData["Listitems"] = rep.GetQuestionList();
                    return View(model);
                }
            }
            else
            {
                return View("Not Found");
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditQuestion(Question q)
        {
            FormCollection check = new FormCollection(Request.Form);
            if (!string.IsNullOrEmpty(check["ArticleBase.InformationBase.headline"]) && !string.IsNullOrEmpty(check["ArticleBase.InformationBase.context"]))
            {
                rep.UpdateQuestion(q);

                return RedirectToAction("SogS");
            }
            else
            {
                ViewData["Listitem"] = rep.GetQuestionList();
                return View(q);
            }
        }

        [HttpGet]
        [Authorize(Roles="Specialists, Administrators")]
        public ActionResult EditAnswer(int? ID)
        {
            if(ID != null)
            {
                var model = rep.GetQuestionByID(ID.Value);

                if(model == null)
                {
                    return View("Not Found");
                }
                else
                {
                    ViewData["Listitems"] = rep.GetQuestionList();
                    return View(model);
                }
            }
            else
            {
                return View("Not Found");
            }
        }

        [HttpPost]
        [Authorize(Roles="Specialists, Administrators")]
        [ValidateInput(false)]
        public ActionResult EditAnswer(Question q)
        {
            
            FormCollection check = new FormCollection(Request.Form);
            if (!string.IsNullOrEmpty(check["answer"]))
            {
                q.specialistID = (rep.GetUserByName(User.Identity.Name)).ID;
                rep.UpdateAnswer(q);
                return RedirectToAction("SogS");
            }
            else
            {
                ViewData["Listitems"] = rep.GetQuestionList();
                return View(q);
            }
        }
       
        [HttpGet]
        public ActionResult GetQuestionByCategory(ArticleCategory category)
        {
            var result = rep.GetQuestionByCategory(category);
            return View(result);
        }

        [HttpGet]
        [Authorize(Roles="Specialists, Administrators")]
        public ActionResult GetUnansweredQuestions()
        {
            IEnumerable<Question> result = rep.GetUnansweredQuestions();
            return View(result);
        }

        [HttpGet]
        public ActionResult GetQuestionByCategoryID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetQuestionByCategoryID(ID.Value);
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public ActionResult GetQuestionByCategoryIDHeadlineAlphabetical(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetQuestionByCategoryIDHeadlineAlphabetical(ID.Value);
                if (result == null)
                {
                    return View("Not Found");
                }
                return View(result);
            }
            return View("Not Found");
        }

        [HttpGet]
        public static IEnumerable<ArticleCategory> GetQuestionCategories()
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetQuestionCategories();
            return result;
        }

        [HttpGet]
        public static int GetAllQuestionsCount()
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetAllQuestions();
            int count = result.Count();
            return count;
        }

        [HttpGet]
        public ActionResult Get10QuestionsByPageID(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.Get10QuestionsByPageID(ID.Value);
                ViewData["pageID"] = ID;
                if (result == null)
                    return View("Not Found");
                return View(result);
            }
            return View("Not Found");
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult DeleteQuestion(int? ID)
        {
            if (ID.HasValue)
            {
                var result = rep.GetQuestionByID(ID.Value);
                rep.DeleteQuestion(result);
                if (result == null)
                    return View("Not Found");
                return View("SogS");
            }
            return View("Not Found");
        }

        #endregion

        #region Surveys

        [HttpGet]
        public ActionResult AllSurveys()
        {
            var result=rep.GetAllSurveys();
            return View(result);
        }

        [HttpGet]
        [Authorize(Roles="Administrators")]
        public ActionResult AddSurvey()
        {
            return View(new Survey());
        }

        [HttpPost]
        [Authorize(Roles="Administrators")]
        public ActionResult AddSurvey(SurveyContainer formData)
        {
            Survey s=new Survey();

            if(ModelState.IsValid)
            {
                rep.CreateSurvey(formData);
                return RedirectToAction("Index");
            }
            else
            {
                return View(formData);
            }
        }

        [HttpPost]
        [Authorize(Roles="Administrators")]
        public ActionResult DeleteSurvey(int? ID)
        {
            var result=rep.GetSurveyByID(ID.Value);
            rep.DeleteSurvey(result);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public static SurveyContainer GetCurrentSurvey()
        {
            DataRepository rep=new DataRepository();
            return rep.GetCurrentSurvey();
        }

        [HttpPost]
        public ActionResult UpdateSurvey(FormCollection formData)
        {
            var ID = int.Parse(formData["ID"]);
            IEnumerable<Choice> result = rep.UpdateChoices(ID, rep.GetUserByName(User.Identity.Name));
            List<Choice> result2 = new List<Choice>();
            foreach (var choice in result)
            {
                result2.Add(new Choice { choice1 = choice.choice1, count = choice.count });
            }
            result = result2;
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        
        [HttpGet]
        public static bool HasUserVoted(string userName)
        {
            DataRepository rep = new DataRepository();
            User user = rep.GetUserByName(userName);
            bool result = rep.HasUserVoted(user.ID);
            return result;
        }

        [HttpGet]
        public ActionResult GetChoices()
        {
            IEnumerable<Choice> result = rep.GetChoicesBySurveyID(((rep.GetCurrentSurvey()).survey).ID);
            List<Choice> result2 = new List<Choice>();
            foreach (var choice in result)
            {
                Choice thing = new Choice { count = choice.count, choice1 = choice.choice1 };
                result2.Add(thing);
            }
            result = result2;
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Users

        public static void RegisterUser(int age, bool gender, string description, string userName, string fullName, string address, int zip, string email)
        {
            DataRepository rep = new DataRepository();
            User user = new User { age = age, address = address, description = description, gender = gender, email = email, name = fullName, userName = userName, zipCode = zip };
            rep.CreateUser(user);
        }

        public static void RegisterSpecialistUser(int age, bool gender, string description, string userName, string fullName, string address, int zip, string email, int category)
        {
            DataRepository rep = new DataRepository();
            User user = new User { gender = gender, email = email, description = description, age = age, address = address, name = fullName, zipCode = zip, userName = userName };
            SpecialistUser spUser = new SpecialistUser { specialistCategoryID = category };
            rep.AddSpecialistUser(user, spUser);
        }
        
        #endregion

        #region Comments

        [HttpGet]
        public ActionResult GetComments(int? id)
        {
            IEnumerable<Comment> comments = rep.GetCommentsByID(id.Value);
            List<CommentContainer> commentCollection = new List<CommentContainer>();
            foreach (var comment in comments)
            {
                commentCollection.Add(new CommentContainer { commentDate = comment.commentDate.ToShortDateString(), message = comment.message, userName = comment.User.name, userID=comment.User.ID });
            }
            IEnumerable<CommentContainer> result = commentCollection;
           
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetCommentsByUserID(int? id)
        {
            IEnumerable<Comment> comments = rep.GetCommentsByUserID(id.Value);
            List<CommentContainer> commentCollection = new List<CommentContainer>();
            foreach (var comment in comments)
            {
                commentCollection.Add(new CommentContainer { commentDate = comment.commentDate.ToShortDateString(), message = comment.message, userName = comment.User.name, userID = comment.User.ID, url=comment.ArticleBase.link, artyID=comment.ArticleBase.informationBaseID });
            }
            IEnumerable<CommentContainer> result = commentCollection;

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [Authorize(Roles = "Administrators, Specialists, PublicUser")]
        public ActionResult AddComment(FormCollection formData)
        {
            Comment comment = new Comment { message = formData["message"], articleBaseID = int.Parse(formData["articleID"]), userID = (rep.GetUserByName(User.Identity.Name)).ID, commentDate = DateTime.Now };
            rep.AddComment(comment);
            IEnumerable<Comment> comments = rep.GetCommentsByID(int.Parse(formData["articleID"]));
            List<CommentContainer> commentCollection = new List<CommentContainer>();
            foreach (var currentComment in comments)
            {
                commentCollection.Add(new CommentContainer { commentDate = currentComment.commentDate.ToShortDateString(), message = currentComment.message, userName = currentComment.User.name, userID=comment.User.ID });
            }
            IEnumerable<CommentContainer> result = commentCollection;
            
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public static int GetCommentsCountByArticleBaseID(int ID)
        {
            DataRepository rep = new DataRepository();
            var result = rep.GetCommentsByID(ID);
            int count = result.Count();
            return count;
        }

        #endregion

        #region Testing
        // smiðir

        public HomeController()
        {
            rep = new DataRepository();
        }

        public HomeController(IDataRepository repos)
        {
            rep = repos;
        }

        #endregion
    }
} 